﻿using Comm.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Comm.DAL
{
  class common
 {
      #region a01.将数据表转换成泛型集合接口+ IList<MODEL.MgrMenu> Table2List(DataTable dt)
        /// <summary>
        /// a01.将数据表转换成泛型集合接口
        /// </summary>
        /// <param name="dt">数据表对象</param>
        /// <returns>泛型集合接口</returns>
     public class TBToList<T> where T : new()
      {
        public static IList<T> Table2List(DataTable dt)
        {
           IList<T> ts = new List<T>();// 定义集合
           Type type = typeof(T); // 获得此模型的类型
           string tempName = "";
           foreach (DataRow dr in dt.Rows)
            {
               T t = new T();
               PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
               foreach (PropertyInfo pi in propertys)
                {
                   tempName = pi.Name;
                   if (dt.Columns.Contains(tempName))
                     {
                       if (!pi.CanWrite) continue;
                       object value = dr[tempName];
                       if (value != DBNull.Value)
                         pi.SetValue(t, value, null);
                     }
                }
               ts.Add(t);
           }
        return ts;
      }
   }  
  #endregion
 }
}
